<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
		
/**
 * This LIB file is making image upload to imageshack.us more easy.
 * Note: This is my first public release to CI community
 *
 * more info: http://www.sourcer.cz/ci-lib/imageshack
 *
 * Copyright (c) 2010 Daniel Máslo (http://www.sourcer.cz)
 *
 * This source file is subject to the GPL license.
 */
class Imageshack extends UploadFiles{
	/** API URL */
	private $api = 'http://www.imageshack.us/upload_api.php';
	
	/** parametros de la api **/
	private $optsize = null;
	private $a_username = 'pelimaniaco';
	private $a_password = '1nd13ds';
	private $developer_key = 'J73120WZ352819f1775f35866ea5c28fd67eb9be';
	
	/** XML response */
	private $big_xml;
	private $medium_xml;
	private $small_xml;
	private $_xml;
	
	/** 
	 * empty contructor 
	 */
	function __construct() {
		parent::__construct();
	}
			 
	/**
	 * Sube una imagen a Imageshack con los tamaños espesificados ('big','medium','small')
	 * Puedes usar cualquier path, de tu servidor o de algun otro.
	 *
	 * @params $params = array(
	 * 		'file' => '', //url de la imagen a cargar o nombre del parametro $_FILE
			'img_temp' => FALSE, //si es una url = false, si es nombre de $_File = true
			'size' => array('big','medium','small', 'temp') //almenos uno de ellos ordenados de esa forma
	 * );
	 * @return boolean
	 */
	public function upload($params) {
		$result_up = $this->uploadFile($params);
		
		if(is_array($result_up)){
		    $this->_checkCurl();
		    
			$ch = curl_init($this->api);
		    
			foreach ($params['size'] as $key => $itm) {
				$url = $this->api.
					'?url='.$result_up[$itm]['url'].
					'&a_username='.$this->a_username.
					'&a_password='.$this->a_password.
					'&key='.$this->developer_key;
				
				curl_setopt($ch, CURLOPT_URL, $url);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				
				if(property_exists($this, $itm.'_xml'))
					$this->{$itm.'_xml'} = curl_exec($ch);
				else
					$this->_xml = curl_exec($ch);
				
				$this->deleteFile($result_up[$itm]['path']);
			}
		    curl_close($ch);
			
			return true;
		}
		return FALSE;
	}	
		    
	/**
	 * parsing XML response for info about uploaded file
	 *
	 * useable tags keys) :
	 *      image_link  - URL address of uploaded image on imageshack server
	 *      thumb_link  - URL address of thumbnail of uploaded image on imageshack server
	 *      yfrog_link  - URL address of uploaded image on yfrog server
	 *      yfrog_thumb - URL address of thumbnail of uploaded image on yfrog server  
	 *      ad_link     - URL address of imgaeshack page with uploaded image
	 *      done_page   - URL address of thumbnail of uploaded image on imageshack server
	 *      width       - width of uploaded image [px]
	 *      height      - height of uploaded image [px]
	 *
	 * @key string XML tag name (see documentation)
	 * @size string uno de los tamaños 'big','medium','small'
	 * @return string text value of param tag
	 */
	function getInfo($key, $size) {
	    preg_match('/<'.$key.'>(.*)<\/'.$key.'>/', (property_exists($this, $size.'_xml')? $this->{$size.'_xml'}: $this->_xml), $value);
	    
	    if (empty($value[0])) {
	        return('invalid key');
	    } else {
	        return(strip_tags($value[0]));
	    }
	}
	
	function getXml($size) {
		if(isset($this->{$itm.'_xml'}))
	    	return $this->{$size.'_xml'};
		else
			return $this->_xml;
	}
	
	/**
	 * private function - checking CURL module
	 */ 
	function _checkCurl() {
	    function_exists('curl_init') or die("curl library is still not installed yet on your machine!");
	}
    
	
}


?>